<?php
defined('IN_TS') or die('Access Denied.');

//课程页
$studyid = intval($_GET['id']);

//课程信息
$strStudy = $new['study']->getOneStudy($studyid);

if($strStudy['studyid'] == '' || $strStudy['isaudit']==1 && $TS_USER['isadmin']==0){
	header ( "HTTP/1.1 404 Not Found" );
	header ( "Status: 404 Not Found" );
	$title = '404';
	include pubTemplate ( "404" );
	exit ();
}

$strStudy['user'] = aac('user')->getOneUser($strStudy['userid']);


/////////////////分类导航开始/////////////////
//一级分类信息
$oneCate = $new['study']->find('study_cates',array(
	'cateid'=>$strStudy['cateid'],
));
//二级分类列表
$arrTwoCate = $new['study']->findAll('study_cates',array(
	'recateid'=>$oneCate['cateid'],
));
/////////////////分类导航结束/////////////////



//获取小组的分类
$strStudy['onecate'] = $new['study']->find('study_cates',array(
	'cateid'=>$strStudy['recateid'],
));
$strStudy['twocate'] = $new['study']->find('study_cates',array(
	'cateid'=>$strStudy['cateid'],
));




if($TS_USER['userid']){
	$userid = $TS_USER['userid'];
	//用户是否喜欢该课程
	$userIsLove = $new['study']->findCount('study_love',array(
		'userid'=>$userid,
		'studyid'=>$studyid,
	));

}


//判断用户学习状态,未学习为空，学习了获取学习信息
$strStudy['learn'] = '';
if(intval($TS_USER['userid'])){
	//用户学习状态
	$strStudy['learn'] = $new['study']->find('study_learn',array(
		'userid'=>intval($TS_USER['userid']),
		'studyid'=>$strStudy['studyid'],
	));
	//用户已经学课时
	$arrLessonUsers = $new['study']->findAll('study_lesson_user',array(
		'userid'=>intval($TS_USER['userid']),
		'studyid'=>$studyid,
	));
	
	foreach($arrLessonUsers as $key=>$item){
		$arrIsLearn[$item['lessonid']] = 1;//已学课时
	}
	
}



//课程下课时列表
$arrLesson = $new['study']->findAll('study_lesson',array(
	'studyid'=>$studyid,
),'orderid asc');

//课程下的学员 
$arrLearnUsers = $new['study']->findAll('study_learn',array(
	'studyid'=>$studyid,
),'addtime desc',null,16);

foreach($arrLearnUsers as $key=>$item){

    $isUser = aac('user')->isUser($item['userid']);

    if($isUser){
        $arrLearnUser[] = aac('user')->getOneUser($item['userid']);
    }else{
        $new['study']->delete('study_learn',array(
            'userid'=>$item['userid'],
            'studyid'=>$item['studyid'],
        ));
    }


}


//相关课程
$arrRelatedStudys = $new['study']->findAll('study',array(
	'cateid'=>$strStudy['cateid'],
	'isaudit'=>0,
),'addtime desc',null,3);

foreach($arrRelatedStudys as $key=>$item){
	$arrRelatedStudy[] = $item;
	$arrRelatedStudy[$key]['user'] = aac('user')->getOneUser($item['userid']);
}


//统计浏览数
$new['study']->update('study', array(
	'studyid'=>$studyid,
), array(
	'count_view' => $strStudy['count_view'] + 1,
));

$sitekey = $strStudy['studyname'];
$sitedesc = cututf8(t($strStudy['studydesc']),0,100);

$title = $strStudy['studyname'];
include template('show');